<template>
  <div>
    <div class="votingBox">
      <div class="votingHeader">
        <el-row :gutter="25">
          <el-col :span="12">
            <el-button
              @click="select(0)"
              :class="[tabs == 0 ? 'btnsSelect' : 'btns']"
              round
              >工作投票</el-button
            >
          </el-col>
          <el-col :span="12">
            <el-button
              @click="select(1)"
              :class="[tabs == 1 ? 'btnsSelect' : 'btns']"
              round
              >问卷调查</el-button
            >
          </el-col>
        </el-row>
        <el-button class="addbtnes" plain @click="addVoting"
          >新增投票</el-button
        >
        <el-button class="addbtns" plain @click="addQuest">新增问卷</el-button>
      </div>
      <div class="votingMain" v-if="this.tabs == 0">
        <noMore v-if="votedatas.length == 0"></noMore>

        <div class="votingList" v-for="(item, i) in votedatas" :key="i">
          <div class="leftBox">
            <div class="title">
              <div class="titleText">{{ item.title }}</div>
            </div>
            <div class="leftBottom">
              <el-button class="leftBottomBtn" @click="ToVoint(item._id)" plain
                >投票</el-button
              >
              <div class="vointTxt">{{ item.voteNum }}个投票</div>
            </div>
          </div>
          <div class="rightBox">
            <div>截止于{{ item.endtime | Time }}</div>
            <div class="yonghu" v-if="item.Creat_people && !item.anonymous">
              <img :src="item.Creat_people.head_portrait | imgF" class="img" />
              {{ item.Creat_people | name }}
            </div>
            <div class="yonghu" v-if="!item.Creat_people">
              <span style="font-weight: 550;">官方发布</span>
            </div>
          </div>
        </div>
        <div class="invitationPagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :page-size="limitSize"
            layout="prev, pager, next, jumper"
            :total="RecordeSize"
          ></el-pagination>
        </div>
      </div>
      <div class="votingMain" v-else>
        <noMore v-if="questiondata.length == 0"></noMore>

        <div class="votingList" v-for="(item, i) in questiondata" :key="i">
          <div class="leftBox">
            <div class="title">
              <div class="titleText">{{ item.title }}</div>
            </div>
            <div class="leftBottom">
              <el-button
                class="leftBottomBtn"
                @click="ToQuestion(item._id)"
                plain
                >填写</el-button
              >
              <div class="vointTxt">
                {{ item.questionNum }}人已完成该问卷调查
              </div>
            </div>
          </div>
          <div class="rightBox">
            <div>截止于{{ item.endtime | Time }}</div>
            <div class="yonghu" v-if="item.Creat_people && !item.anonymous">
              <img :src="item.Creat_people.head_portrait | imgF" class="img" />
              {{ item.Creat_people | name }}
            </div>
            <div class="yonghu" v-if="!item.Creat_people">
              <span style="font-weight: 550;">官方发布</span>
            </div>
          </div>
        </div>
        <div class="invitationPagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :page-size="limitSize"
            layout="prev, pager, next, jumper"
            :total="RecordeSize"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get_question } from "../../api/question";
import { get_vote } from "../../api/vote";
import noMore from "../../components/noMore";
import { formatDateTime } from "../../common/Time";
import { defaultAvatarURL } from "../../utils/global";

export default {
  name: "workVoting",
  components: {
    noMore
  },
  filters: {
    Time(val) {
      return formatDateTime(val);
    },
    name(val) {
      if (val) {
        return val.name;
      } else {
        return "";
      }
    },
    imgF(val) {
      return defaultAvatarURL + val;
    }
  },
  data() {
    return {
      list: [1, 2, 3, 4, 5, 6],
      tabs: 0,
      votedatas: [], //投票数据
      questiondata: [], //问卷数据
      skipSize: 0, //开始位置
      RecordeSize: 0, //总文章
      limitSize: 10
    };
  },
  created() {
    switch (this.tabs) {
      case 0:
        (this.skipSize = 0), //开始位置
          this.get_voteData();
        break;
      case 1:
        (this.skipSize = 0), //开始位置
          this.get_questionData();
        break;
      default:
        break;
    }
  },
  methods: {
    addVoting() {
      this.$router.push({
        path: "/addVoting", //跳转路径
        // name: "Voting", //跳转路径的name值，不写跳转后页面取不到参数
        // 参数
        query: {}
      });
    },
    addQuest() {
      this.$router.push({
        path: "/addquestion", //跳转路径
        // name: "Voting", //跳转路径的name值，不写跳转后页面取不到参数
        // 参数
        query: {}
      });
    },
    select(index) {
      if (index == this.tabs) {
        return;
      }
      this.tabs = index;
      switch (index) {
        case 0:
          (this.skipSize = 0), //开始位置
            this.get_voteData();
          break;
        case 1:
          (this.skipSize = 0), //开始位置
            this.get_questionData();
          break;
        default:
          break;
      }
    },
    async get_questionData() {
      var reqdata = {
        query: {
          status: true
        },
        sort: { _id: 1 },
        populate: JSON.stringify([{ path: "Creat_people" }]),
        skip: this.skipSize,
        limit: this.limitSize,
        count: true
      };
      var questiondata = await get_question(reqdata);
      console.log(questiondata.data);
      this.questiondata = questiondata.data;
      this.RecordeSize = questiondata.countnum;
    },
    async get_voteData() {
      var reqdata = {
        query: {
          status: true
        },
        populate: JSON.stringify([{ path: "Creat_people" }]),
        sort: { _id: 1 },
        skip: this.skipSize,
        limit: this.limitSize,
        count: true
      };
      var votedata = await get_vote(reqdata);
      console.log(votedata.data);
      this.votedatas = votedata.data;
      this.RecordeSize = votedata.countnum;
      console.log(this.votedatas);
    },
    ToVoint(val) {
      this.$router.push({
        path: "/Voting", //跳转路径
        // name: "Voting", //跳转路径的name值，不写跳转后页面取不到参数
        // 参数
        query: {
          _id: val
        }
      });
    },
    ToQuestion(val) {
      this.$router.push({
        path: "/Question", //跳转路径
        // name: "Question", //跳转路径的name值，不写跳转后页面取不到参数
        // 参数
        query: {
          _id: val
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.skipSize = (val - 1) * this.limitSize;
      console.log(`当前页: ${val}`);
      switch (this.tabs) {
        case 0:
          this.get_voteData();
          break;
        case 1:
          this.get_questionData();
          break;
        default:
          break;
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.votingBox {
  min-height: 800px;
  width: 100%;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: #ffffff;
  .votingHeader {
    width: auto;
    height: 80px;
    line-height: 20px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding-left: 90px;
    padding-top: 10px;
    border-bottom: 1px solid #eaeaea;
    position: relative;
    .btns {
      border: none;
      display: block;
      color: #000000;
      background-color: #eaeaea;
      font-weight: 550;
      font-size: 16px;
      width: 120px;
      letter-spacing: 2px;
      width: 120px;
    }
    .btnsSelect {
      border: none;
      display: block;
      background-color: #eef5ff;
      color: #3257b5;
      font-weight: 550;
      font-size: 16px;
      width: 120px;
      letter-spacing: 2px;
      width: 120px;
    }
    .addbtns {
      display: block;
      color: #3257b5;
      background-color: #eaeaea;
      font-weight: 550;
      font-size: 14px;
      width: 100px;
      letter-spacing: 2px;
      position: absolute;
      right: 30px;
    }
    .addbtnes {
      display: block;
      color: #3257b5;
      background-color: #eaeaea;
      font-weight: 550;
      font-size: 14px;
      width: 100px;
      letter-spacing: 2px;
      position: absolute;
      right: 160px;
    }
  }
  .votingMain {
    width: 1400px;
    overflow: auto;
    padding: 0 10px;
    .votingList {
      width: 1200px;
      height: 150px;
      margin: 0 auto;
      border-bottom: 1px solid #eaeaea;
      display: flex;
      justify-content: space-between;
      .leftBox {
        width: 600px;
        display: flex;
        flex-direction: column;
        .title {
          height: 80px;
          display: flex;
          align-items: center;
          font-size: 20px;
          font-weight: 550;
          .titleText {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
        .leftBottom {
          display: flex;
          align-items: center;
          .leftBottomBtn {
            width: 120px;
            height: 44px;
            font-size: 18px;
            border: 2px solid #3257b5;
          }
          .vointTxt {
            font-size: 18px;
            color: #c0d1ff;
            margin-left: 24px;
          }
        }
      }
      .rightBox {
        width: 400px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        font-size: 18px;
        color: #505050;
        .yonghu {
          display: flex;
          align-items: center;
          font-size: 15px;
          .img {
            width: 32px;
            height: 32px;
            margin: 0 10px 0 0;
            border-radius: 50%;
          }
        }
      }
    }
    .invitationPagination {
      width: 80%;
      margin: 0 auto;
      height: 80px;
      display: flex;
      align-items: center;
    }
  }
}
</style>
